package de.gameduell.framework.facebook.app.pyramide {
	import de.gameduell.framework.facebook.app.PageManager;
	import de.gameduell.framework.facebook.app.Controller;
	import de.gameduell.framework.facebook.app.popups.PyramidBasePopup;

	import de.gameduell.facebook.service.gen.CatalogItem;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.facebook.FBBaseController;
	import de.gameduell.framework.facebook.communication.ServiceLayerCommunication;
	import de.gameduell.framework.facebook.event.InventoryEvent;
	import de.gameduell.framework.gui.button.Button;
	import de.gameduell.framework.gui.button.ButtonEvent;
	import de.gameduell.framework.gui.popup.Popup;
	import de.gameduell.framework.gui.util.ToolTip;
	import de.gameduell.framework.gui.wrapper.SpriteWrapper;
	import de.gameduell.framework.locale.LocaleData;
	import de.gameduell.framework.util.Library;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;

	public class BrickSelectItem extends SpriteWrapper {
		private var library:Library;
		private var selectButton:Button;
		private var moreFriendsPopup:PyramidBasePopup;
		private var controller:Controller;
		private var localeData:LocaleData;
		private var brickCondition:BrickCondition;
		private var transactionPopup:Popup;
		private var pageManager:PageManager;

		public function BrickSelectItem(aBrickId:String, aBrickCondition:BrickCondition) {
			super(new Sprite());
			
			pageManager = PageManager.getInstance();
			library = Library.getInstance();
			controller = Controller.getInstance();
			localeData = LocaleData.getInstance();
			brickCondition = aBrickCondition;
			
			var buttonClip:Sprite = library.createSprite("itemSelectButton");
			var buttonOverlay:Sprite = buttonClip.getChildByName("overlay") as MovieClip;
			buttonOverlay.mouseEnabled = false;
			buttonOverlay.mouseChildren = false;
			
			var buttonIcon:MovieClip = buttonOverlay.getChildByName("icon") as MovieClip;
			var buttonText:TextField = buttonOverlay.getChildByName("price") as TextField;
			
			if(brickCondition != null) {
				buttonIcon.gotoAndStop(aBrickCondition.condition);
				buttonText.text = aBrickCondition.price;
				Debug.trace("BrickSelectItem::BrickSelectItem price: " + aBrickCondition.brickType + " / " + aBrickCondition.price);
			}
				
			var buttonSprite:Sprite = buttonClip.getChildByName("button") as Sprite;
			
			//			addAndCenterSprite(buttonSprite.getChildByName("up") as Sprite, library.createSprite(aBrickId));
			//			addAndCenterSprite(buttonSprite.getChildByName("over") as Sprite, library.createSprite(aBrickId));
			//			addAndCenterSprite(buttonSprite.getChildByName("down") as Sprite, library.createSprite(aBrickId));
			//			addAndCenterSprite(buttonSprite.getChildByName("disabled") as Sprite, library.createSprite(aBrickId));
			addAndCenterSprite(buttonSprite.getChildByName("up") as Sprite, pageManager.getPicFromLib(aBrickId));
			addAndCenterSprite(buttonSprite.getChildByName("over") as Sprite, pageManager.getPicFromLib(aBrickId));
			addAndCenterSprite(buttonSprite.getChildByName("down") as Sprite, pageManager.getPicFromLib(aBrickId));
			addAndCenterSprite(buttonSprite.getChildByName("disabled") as Sprite, pageManager.getPicFromLib(aBrickId));

			selectButton = new Button(buttonSprite, aBrickId);
			selectButton.sprite.cacheAsBitmap = true;
			
			if(brickCondition != null) {
				switch(aBrickCondition.condition) {
					case "cash":
						if(controller.gameData.userOwnsBrick(aBrickId)) {
							buttonOverlay.visible = false;
							selectButton.addEventListener(ButtonEvent.CLICK, onSelectionDone);
						} else {
							new ToolTip("tooltip", "buyItemForUnlock", selectButton.sprite, true);
							selectButton.addEventListener(ButtonEvent.CLICK, onBuyBrick);
						}
						break;
	
					case "friends":
						if(controller.facebookData.friendProfiles != null) {
							if(aBrickCondition.friendsNeeded > controller.facebookData.friendProfiles.length - 1) {
								selectButton.addEventListener(ButtonEvent.CLICK, onNeedMoreFriends);
								new ToolTip("tooltip", "moreFriendsForUnlock", selectButton.sprite, true);
							} else {
								buttonOverlay.visible = false;
								selectButton.addEventListener(ButtonEvent.CLICK, onSelectionDone);
							}
						} else {
							selectButton.addEventListener(ButtonEvent.CLICK, onNeedMoreFriends);
							new ToolTip("tooltip", "moreFriendsForUnlock", selectButton.sprite, true);
						}
						break;								
							
					case "none":
						buttonOverlay.visible = false;
						selectButton.addEventListener(ButtonEvent.CLICK, onSelectionDone);
						break;
				}
			} else {
				selectButton.active = false;
				buttonClip.alpha = 0.50;
				buttonOverlay.visible = false;
			}
			sprite.addChild(buttonClip);
		}

		private function addAndCenterSprite(baseSprite:Sprite, aSprite:Sprite):void {
			// TODO - errorhandling if sprite is not in lib ?
			aSprite.x = (baseSprite.width - aSprite.width) / 2 - 1;
			aSprite.y = ((baseSprite.width - aSprite.height) / 2) - 13;
			baseSprite.addChild(aSprite);
		}

		private function onSelectionDone(event:ButtonEvent):void {
			dispatchEvent(new ButtonEvent(ButtonEvent.CLICK, selectButton));
		}

		private function onNeedMoreFriends(event:ButtonEvent):void {
			localeData.setValue("friendsCount", String(controller.facebookData.friendProfiles.length));
			moreFriendsPopup = new PyramidBasePopup("generic_2_buttons_header_content", "moreFriendsNeededPopup", 0, true);
			moreFriendsPopup.registerButton("skipButton", moreFriendsPopup.hide);
			moreFriendsPopup.registerButton("okButton", inviteMoreFriends);
			moreFriendsPopup.show();
		}

		private function inviteMoreFriends(event:Event):void {
			moreFriendsPopup.hide();
			controller.inviteFriends();
		}

		private function onBuyBrick(event:ButtonEvent):void {
			Debug.trace("BrickSelectItem::onBuyBrick: " + brickCondition.cashItem, Debug.DEBUG);
			if(transactionPopup == null) {
				transactionPopup = new Popup("cashTransactionPopup", "cashTransactionPopup", 0, false);
				transactionPopup.modal = true;
				transactionPopup.blocker.setColor(0x000000, 0.25);
			}
			transactionPopup.show();
			
			var item:CatalogItem = controller.catalog.getItemByTitle(brickCondition.cashItem);
			Debug.trace("BrickSekectItem::onBuyBrick " + (item == null), Debug.DEBUG);
			controller.addEventListener(FBBaseController.OUT_OF_MONEY, transactionPopup.hide);
			controller.communication.addEventListener(ServiceLayerCommunication.PURCHASE_ITEM_FAILED, onPurchaseFailed);
			controller.inventory.addEventListener(InventoryEvent.INVENTORY_ITEM_ADDED, onInventoryItemAdded);
			controller.buyItem(item);
		}

		private function onPurchaseFailed(event:Event):void {
			Debug.trace("BrickSelectItem::onPurchaseFailed", Debug.WARNING);
			controller.communication.removeEventListener(ServiceLayerCommunication.PURCHASE_ITEM_FAILED, onPurchaseFailed);
			controller.inventory.removeEventListener(InventoryEvent.INVENTORY_ITEM_ADDED, onInventoryItemAdded);
			transactionPopup.hide();
		}

		private function onInventoryItemAdded(event:InventoryEvent):void {
			Debug.trace("BrickSelectItem::onInventoryItemAdded", Debug.CONNECTION);
			controller.communication.removeEventListener(ServiceLayerCommunication.PURCHASE_ITEM_FAILED, onPurchaseFailed);
			if(event.itemId == controller.catalog.getItemByTitle(brickCondition.cashItem).catalogId) {
				controller.inventory.removeEventListener(InventoryEvent.INVENTORY_ITEM_ADDED, onInventoryItemAdded);
				dispatchEvent(new ButtonEvent(ButtonEvent.CLICK, selectButton));
			}
			transactionPopup.hide();
		}
	}
}
